package com.droid.lib.controls.demo.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.avast.android.dialogs.fragment.DatePickerDialogFragment;
import com.avast.android.dialogs.fragment.TimePickerDialogFragment;
import com.avast.android.dialogs.iface.IDateDialogListener;
import com.droid.lib.controls.demo.R;
import com.droid.lib.controls.demo.adapter.MenuAdapter;
import com.droid.lib.controls.demo.adapter.MenuAdapter.OnClicker;
import com.droid.lib.controls.widget.calendardialog.CalendarDatePickerDialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CalendarFragment extends Fragment implements IDateDialogListener{
	private Context mContext;
	private ListView listview;
	private MenuAdapter adapter;
	private TextView text;
	private ArrayList<String> marrStrs = new ArrayList<String>();
	private static final String DATE_PICKER_TAG = "DATE_PICKER_TAG";

	private static final int REQUEST_DATE_PICKER = 12;
	private static final int REQUEST_TIME_PICKER = 13;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mContext = getActivity();
		View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_calendar, null);
		listview = (ListView) view.findViewById(R.id.listview);
		text=(TextView)view.findViewById(R.id.text);
		adapter = new MenuAdapter(mContext);
		listview.setAdapter(adapter);
		marrStrs.add("MaterialCalendar");
		marrStrs.add("Material DatePickerDialog");
		marrStrs.add("Material TimePickerDialog");
		marrStrs.add("Other DatePickerDialog");
		adapter.setList(marrStrs);

		adapter.setOnClicker(new OnClicker() {

			@Override
			public void onclick(int position) {
				
				switch (position) {
				case 0:
					Intent intent = new Intent(mContext, CalendarActivity.class);
					intent.putExtra("type", 1);
					mContext.startActivity(intent);
					break;
				case 1:
					DatePickerDialogFragment
							.createBuilder(mContext, getActivity().getSupportFragmentManager())
							.setDate(new Date())
							.setPositiveButtonText(android.R.string.ok)
							.setNegativeButtonText(android.R.string.cancel)
							.setTargetFragment(CalendarFragment.this,REQUEST_DATE_PICKER)
							.show();

					break;
				case 2:
					TimePickerDialogFragment
							.createBuilder(mContext, getActivity().getSupportFragmentManager())
							.setDate(new Date())
							.setPositiveButtonText(android.R.string.ok)
							.setNegativeButtonText(android.R.string.cancel)
							.setTargetFragment(CalendarFragment.this,REQUEST_TIME_PICKER)
							.show();
					break;
				case 3:
					showDatePicker();
					break;
				case 4:
					
					break;
				}
	
			}
		});
		return view;

	}
	 private void showDatePicker() {
		    Calendar calendar = Calendar.getInstance();
		    int year = calendar.get(Calendar.YEAR);
		    int monthOfYear = calendar.get(Calendar.MONTH);
		    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		    final CalendarDatePickerDialog dialog =
		        CalendarDatePickerDialog.newInstance(mContext,dateSetListener, year, monthOfYear, dayOfMonth);

		    dialog.show(getChildFragmentManager(), DATE_PICKER_TAG);
		  }

		  CalendarDatePickerDialog.OnDateSetListener dateSetListener =
		      new CalendarDatePickerDialog.OnDateSetListener() {
		        @Override
		        public void onDateSet(CalendarDatePickerDialog dialog, int year, int monthOfYear,
		            int dayOfMonth) {
		        	text.setText(
		              String.format("Year: %s, Month: %s, Day: %s", year, monthOfYear + 1,
		                  dayOfMonth));
		        }
		      };

	@Override
	public void onPositiveButtonClicked(int resultCode, Date date) {
		String text = "";
		if (resultCode == REQUEST_DATE_PICKER) {
			text = "Date ";
		} else if (resultCode == REQUEST_TIME_PICKER) {
			text = "Time ";
			DateFormat timeFormat = new SimpleDateFormat("hh:MM:ss");
			Toast.makeText(mContext, text + " " + timeFormat.getCalendar().get(Calendar.HOUR) +
					":"+timeFormat.getCalendar().get(Calendar.MINUTE) +
					":"+timeFormat.getCalendar().get(Calendar.SECOND), Toast.LENGTH_SHORT).show();
			return;
		}

		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT);
		Toast.makeText(mContext, text + " " + dateFormat.format(date), Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onNegativeButtonClicked(int i, Date date) {
		Toast.makeText(mContext,"Cancel", Toast.LENGTH_SHORT).show();
	}
}
